﻿
using System;
using Dora.Services.AdminUser.Entities;
using SqlSugar;
using System.Threading.Tasks;
using Dora.Orm.SqlSugarCore.UnitOfWork;
using Surging.Core.Common;
using Surging.Core.CPlatform.Ioc;
using System.Collections.Generic;
using Dora.Services.AdminUser.Dto.ViewDto;

namespace Dora.Services.AdminUser.Repository.Repositorys
{

    /// <summary>
    /// 美程用户角色映射表仓储层
    /// </summary>
    public class DoraUserRoleMapRepository : BaseRepository
    {
        private ISugarUnitOfWork _unitOfWork;
        public DoraUserRoleMapRepository(ISugarUnitOfWork unitOfWork)
        {
            _unitOfWork = unitOfWork;
        }

        /// <summary>
        /// 通过员工IdList获取所有员工所属的角色,带名字
        /// </summary>
        /// <param name="userIdList"></param>
        /// <returns></returns>
        public async Task<List<DoraUserRoleMapListDto>> GetListByUserIdList(List<Guid> userIdList)
        {
            var result = await _unitOfWork.QueryClient.Queryable<DoraUserRoleMapEntity, RoleEntity>((urm, r) => new object[] {
                JoinType.Inner, urm.RoleId == r.Id,
            })
                .Where((urm, r) => userIdList.Contains(urm.UserId))
                .Select((urm, r) => new DoraUserRoleMapListDto{ UserId= urm.UserId, RoleId=urm.RoleId,RoleName= r.Name }).ToListAsync();
            return result;
        }

        public async Task<int> DeleteByUserIdList(List<Guid> userIdList)
        {
            var result = await _unitOfWork.ModifyClient.Deleteable<DoraUserRoleMapEntity>().Where(p => userIdList.Contains(p.UserId)).ExecuteCommandAsync();

            return result;
        }

        public async Task<int> CreateRange(List<DoraUserRoleMapEntity> list)
        {
            var result =await _unitOfWork.ModifyClient.Insertable(list).ExecuteCommandAsync();
            return result;
        }

        /// <summary>
        /// 通过角色Id获取权限所属的员工,带名字
        /// </summary>
        /// <param name="EmployeeIdList"></param>
        /// <returns></returns>
        public async Task<List<DoraUserRoleMapListDto>> GetDoraListByRoleIdList(List<Guid> RoleIdList)
        {
            var result = await _unitOfWork.QueryClient.Queryable<DoraUserRoleMapEntity, DoraUserEntity>((erm, e) => new object[] {
                    JoinType.Inner, erm.UserId == e.Id,
                })
                .Where((erm, e) => RoleIdList.Contains(erm.RoleId) && e.IsDeleted == 0)
                .Select((erm, e) => new DoraUserRoleMapListDto { UserId = erm.UserId, RoleId = erm.RoleId, RealName = e.RealName }).ToListAsync();
            return result;
        }

        /// <summary>
        /// 通过角色Id获取权限所属的员工
        /// </summary>
        /// <param name="EmployeeIdList"></param>
        /// <returns></returns>
        public async Task<List<DoraUserRoleMapEntity>> GetListByRoleIdList(List<Guid> RoleIdList)
        {
            var result = await _unitOfWork.QueryClient.Queryable<DoraUserRoleMapEntity>()
                .Where(it => RoleIdList.Contains(it.RoleId))
                .ToListAsync();
            return result;
        }
    }
}
